/**
 * Created by will.jiang on 2017/9/29.
 * 把html文件转换为js文件
 */
var fs = require('fs');
var path = require('path');
var sourceDir = path.join(__dirname,'../html');
//读取文件
var files = fs.readdirSync(sourceDir);
//排序 files [0.html,1.html,10.html]

var reg = /\d+/;
files = files.sort(function(a,b){
    var na = reg.exec(a);
    var nb = reg.exec(b);
    return na-nb
});

console.log(files);
var rs = `//create at:${new Date()}\r\n`;
files.forEach(function(filename){
    var data = fs.readFileSync(path.join(sourceDir,filename),'utf-8');
    //压缩内容
    rs += 'box['+filename.replace('.html','') +'].template =\''+zip(data)+'\';\n\n';
});

//处理完后导出文件
fs.writeFileSync(path.join(__dirname,'../dist/data.js'),rs);
/*
* @author will.jiang
* 日期 2017 09 29 15:56
* 功能描述 去除回车换行。把内容缩成一行
* @param {num} 参数1说明
* @return {num} 返回值说明
* */
function zip(str){
    console.log(str);
    return str.replace(/\r\n/g,'').replace(/{{ cur }}/g,"{{ ($cur) }}");
}
